#include "ICETask.h"

namespace  ICE
{
    OsgTask::~OsgTask()
    {

    }

    void OsgTask::doTask(dw::ThreadPool* pThreadPool)
    {
        auto renderFun=[](void* ptr)
        {
            jobData* pd=(jobData*)ptr;
            osgViewer::ViewerBase* pView=pd->pView;
            pView->frame();
            if(!pView->done())
                pd->pThreadPool->enqueue(pd->pTask);
        };

        _task=pThreadPool->allocate();
        _task->function=renderFun;
        _data= dw::task_data<jobData>(_task);

        osgViewer::Viewer* pViewer =new osgViewer::Viewer;
        _data->pSceneRoot=new osg::Group;
        osg::Node* pNode=osgDB::readNodeFile("cow.osg");
        _data->pSceneRoot->addChild(pNode);
        pViewer->setSceneData(_data->pSceneRoot);
        pViewer->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
        pViewer->setKeyEventSetsDone(0);
        pViewer->setUpViewInWindow(200,200,800,600);
        pViewer->addEventHandler(new osgViewer::StatsHandler);
        pViewer->setCameraManipulator(new osgGA::OrbitManipulator);
        pViewer->realize();

        _data->pView=pViewer;
        _data->pThreadPool=pThreadPool;
        _data->pTask=_task;

        pThreadPool->enqueue(_task);
    }

    dw::Task*  OsgTask::getTask()
    {
         return _task;
    }

    void* OsgTask::getData()
    {
         return _data;
    }


} // namespace  ICE
